<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * View Profile
 * File: viewprofile.php
 * Purpose: Showing a users profile
 * Todo: Add cache support
 *
 */
				
require('system/files/global.php');

// if this a real attempt or a hack attempt?
if(is_numeric($_GET['id']) && isset($_GET['id'])) {
	// is a real request
	// does the ID exist?
	// MAY NEED TO OPTIMISE THE QUERY BELOW AS WITH A VERY ACTIVE USER OR LARGE DATABASE COULD GENERATE SLOWLY...
	$sql = "SELECT `user_id`, 
				   `user_name`, 
				   `user_level`, 
				   `user_language`, 
				   `user_configurations`
			FROM `nbb_users`
			WHERE `user_id` = '" . Db_Abstraction::clean($_GET['id']) . "'
			LIMIT 1";
	$result = Db_Abstraction::fetch_array($sql);

	if($result['user_id'] === NULL) {
		// user doesn't exist
		$core_template_system->setvar('page_title', 'Profile');
		
		// show the header loaded
		$core_template_system->pparse();
		
		// add this language file
		$core_lang_system->reset();
		$core_lang_system->add_file('profiles');
		
		// build the menu
		$core_template_system->setvar('breadcrumb', $core_nav_system->generateNav(array('settings' => array('level_down' => false,
																											'main_class' => 'menu'),
																						'information' => array('home' => array('data' => array(1 => 'Home',
																																		 	   2 => 1))))));
		// redirect them to the homepage
		$core_template_system->setvar(array('error_message' => $core_lang_system->get_var('NO_SUCH_USER', 3),
											'error' => $core_lang_system->get_var('ERROR', 4),
											'back_url' => 'index.php'));

		// render the page
		$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
		$core_template_system->pparse();
	} else {
		// user does exist
		$core_template_system->setvar('page_title', $result['user_name'] . '\'s Profile');
		
		// show the header loaded
		$core_template_system->pparse();
		
		// add this language file
		$core_lang_system->reset();
		$core_lang_system->add_file('profiles');
		
		// is this already cached? and if so, can we use it?
		// cache for 2 days
		if(!CacheEngine::isCacheOn() || CacheEngine::CacheExist('my-profile-' . $_GET['id'], 172800) === false) {
			// generate queries
			// determine total number of topics
			$sql = "SELECT COUNT(`topic_id`) as `nbb_topics`
					FROM `nbb_topics`
					WHERE `topic_starter` = '" . Db_Abstraction::clean($_GET['id']) . "'
					LIMIT 1";
			$result = array_merge($result, Db_Abstraction::fetch_array($sql));
			
			// determine total number of posts
			$sql = "SELECT COUNT(`post_id`) as `nbb_posts`
					FROM `nbb_posts`
					WHERE `post_user_id` = '" . Db_Abstraction::clean($_GET['id']) . "'
					LIMIT 1";
			$result = array_merge($result, Db_Abstraction::fetch_array($sql));

			// set user array
			$template_user_array = array();
			
			// build the menu
			$template_user_array = array_merge($template_user_array, 
									  array('breadcrumb'  => $core_nav_system->generateNav(array(
											'settings'    => array('level_down' => false,
																   'main_class' => 'menu'),
											'information' => array('home' => array('data' => array(1 => 'Home',
																								   2 => 1)),
																   'profile' => array('data' => array(1 => $result['user_name'] . '\'s Profile',
																									  2 => 2,
																									  3 => array('user_id' => $result['user_id'],
																												 'user_name' => $result['user_name']))))))));
	
			// do we have an avatar?
			$user_avatar = file_exists('storedfiles/avatars/' . $result['user_name'] . '.png');
			
			// get user configurations
			$user_config = unserialize($result['user_configurations']);
			
			// is twitter enabled
			if(GlobalSettings::getSetting('twitter_enabled') == '1') {
				// does this user want to use twitter?
				if(!empty($user_config['twitter_name'])) {
					// is enabled
					$twitter_en = true;
				
					// class twitter class and set data
					require(BASE_PATH . '/system/classes/twitter_class.php');
					$twitter = new Twitter;
					
					// set info
					$twitter->setName($user_config['twitter_name']);
					
					// get status info
					$twit_info = $twitter->getTwitterInfo();
					
					if($twit_info === false) {
						// failed
						$twitter_en = false;
					} else {
						// was successful
						$template_user_array = array_merge($template_user_array, 
														   array('last_status' => $twit_info['last_status'],
																 'profile_image' => $twit_info['profile_image'],
																 'followers' => $twit_info['followers'],
																 'twitter_name' => $twit_info['screen_name'])
														   );
						
						// can we update the background?
						if(GlobalSettings::getSetting('twitter_profile_background') == '1' && $user_config['twitter_use_background'] === true && !empty($twit_info['profile_background_image'])) {
							// can use  background
							$template_user_array = array_merge($template_user_array, 
															   array('use_twitter_background' => true,
																	 'twitter_background_img' => $twit_info['profile_background_image'])
															   );
						}
					}
				} else {
					// turn twitter off
					$twitter_en = false;
				}
			} else {
				// isn't enabled
				$twitter_en = false;
			}
			
			// add variables for this profile
			$template_user_array = array_merge($template_user_array,
											   array('username' => $result['user_name'],
													 'has_avatar' => $user_avatar,
													 'twitter_enabled' => $twitter_en,
													 'user_posts' => $result['nbb_posts'],
													 'user_topics' => $result['nbb_topics'])
											   );
			
			// are we permitted to use dailybooth?
			if(GlobalSettings::getSetting('dailybooth_enabled') == 1 && !empty($user_config['dailybooth_name'])) {
				// dailybooth stuff
				require(BASE_PATH . '/system/classes/dailybooth_class.php');
				$dailybooth = new Dailybooth;
				
				// get my dailybooth pics
				$dailybooth->username = $user_config['dailybooth_name'];
				$db_items = $dailybooth->getItems();
				
				// are there more than 0?
				if(count($db_items) > 0 && $db_items !== false) {
					// are some results, show them
					$core_template_system->setloop('dailybooth_pics', $db_items);
					
					// tell the template engine that dailybooth is enabled
					$template_user_array = array_merge($template_user_array, array('dailybooth_enabled' => true,
																				   'dailybooth_items' => $db_items));
				}
			}
			
			// is the cache on?
			if(CacheEngine::isCacheOn()) {
				// add this new info into the database
				CacheEngine::addCache('my-profile-' . $_GET['id'], $template_user_array);
			}
		} else {
			// we can load this data from the cache
			$template_user_array = CacheEngine::fetchCache('my-profile-' . $_GET['id']);
			
			// can we use DB?
			if($template_user_array['dailybooth_enabled']) {
				// set the DB loop
				$core_template_system->setloop('dailybooth_pics', $template_user_array['dailybooth_items']);
			}
		}
		
		// pass to template engine
		$core_template_system->setvar($template_user_array);
		
		// render the page
		$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/profile.html');
		$core_template_system->pparse();
	}
} else {
	// something didn't quite match, could be a hack attempt
	SystemSettings::addNewAttempt($_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI'], 'hack', array('id' => $_GET['id']));
	
	// load the header
	$core_template_system->setvar('page_title', 'Profile');
	
	// show the header loaded
	$core_template_system->pparse();
	
	// include the language file for this page
	$core_lang_system->reset();
	$core_lang_system->add_file('profiles');
	
	// build the menu
	$core_template_system->setvar('breadcrumb', $core_nav_system->generateNav(array('settings' => array('level_down' => false,
																										'main_class' => 'menu'),
																					'information' => array('home' => array('data' => array(1 => 'Home',
																																		   2 => 1))))));

	// redirect them to the homepage
	$core_template_system->setvar(array('error_message' => $core_lang_system->get_var('NO_SUCH_USER', 3),
										'error' => $core_lang_system->get_var('ERROR', 4),
										'back_url' => 'index.php'));
	
	// render the page
	$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
	$core_template_system->pparse();
}

// display the footer
require('system/files/footer.php');
?>